package com.taplinker.client.crm.domain;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.taplinker.client.AppConfigFactory;
import com.taplinker.client.core.DataManager;
import com.taplinker.client.crm.rpc.AssignCustomer;
import com.taplinker.client.crm.rpc.GetAppCategory;
import com.taplinker.client.crm.rpc.GetChatHistory;
import com.taplinker.client.crm.rpc.GetClientTimeLine;
import com.taplinker.client.crm.rpc.GetCustomerById;
import com.taplinker.client.crm.rpc.GetCustomerTimeLine;
import com.taplinker.client.crm.rpc.GetCustomersCmd;
import com.taplinker.client.crm.rpc.UpdateCustomer;
import com.taplinker.core.Resource;
import com.taplinker.core.ResourceManager;
import com.taplinker.dto.web.CrmCategoryProto.CrmCategory;
import com.taplinker.dto.web.CustomerProto.CustomerList_V2;
import com.taplinker.dto.web.CustomerProto.Customer_V2;
import com.taplinker.dto.web.MessageListResponse.MessageListRes;

/**
 * 用户信息中心
 * @author djbian
 *
 */
public class CrmDataCenter extends DataManager implements Resource {
	public static final String GET_CLIENT_TIME = "GET_CLIENT_TIME";
	public static final String GET_CHAT_HISTORY = "GET_CHAT_HISTORY";
	public static final String GET_CUSTOMER = "GET_CUSTOMER";
	public static final String ASSIGN_CUSTOMER = "ASSIGN_CUSTOMER";
	public static final String TRANSFER_CUSTOMER = "TRANSFER_CUSTOMER";
	public static final String GET_CUSTOMERS_CMD = "GET_CUSTOMERS_CMD";
	public static final String GET_CUSTOMER_TIMELINE = "GET_CUSTOMER_TIMELINE";
	public static final String GET_APP_CATEGORY_APP = "GET_APP_CATEGORY_APP";
	public static final String GET_APP_CATEGORY_INDUSTRY = "GET_APP_CATEGORY_INDUSTRY";
	public static final String GET_MATCH_CUSTOMER = "GET_MATCH_CUSTOMER";
	public static final String UPDATE_CUSTOMER = "UPDATE_CUSTOMER";
	public static final String REPLY_WEIBO = "REPLY_WEIBO";
	public static final String ADD_REMARK = "ADD_REMARK";

	private GetCustomerTimeLine getCustomerTimeLine;
	private GetClientTimeLine getClientTimeLine;
	private Context context; // 用于在crm.activity和crm.rpc的类中间传递Context给rpc的Toast
	private CustomerList_V2 customerList;
	private Customer_V2 customer;
	// //////////////////////////////////////////////////////////////////////////////////
	// setIds(((String) result.object).split(","))
	// 在UpdateCustomer里set，在EditCustomerActivity里get
	private String[] ids;
	// MessageListResponse.MessageListRes.parseFrom
	// 在GetChatHsitory里set，在consulHistoryActivity里get
	private MessageListRes messageLists;
	// CrmCategoryProto.CrmCategory.parseFrom
	// 在GetAppCategory里set，在EditCustomerActivity里get
	private CrmCategory crmCategory;
	// 在CustomerDetailActivity中set，在AddRemarkActivity,
	// CompanyWorkLineActivity, CustomerDistributeActivity,
	// CustomerInforActivity,
	// CustomerTimeLineActivity中get的Temp
	private CustomerInfo temp;
	private static CrmDataCenter instance;

	private CrmDataCenter() {
	}

	public static CrmDataCenter getInstance() {
		if (instance != null)
			return instance;
		synchronized (CrmDataCenter.class) {
			if (instance == null) {
				instance = new CrmDataCenter();
				ResourceManager.getInstance().addResource(instance);
			}
		}
		return instance;
	}

	public boolean isRole(String s) {
		return AppConfigFactory.getInstance().getUserInfo().isRole(s);
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public GetCustomerTimeLine getGetCustomerTimeLine() {
		return getCustomerTimeLine;
	}

	public void setGetCustomerTimeLine(GetCustomerTimeLine getCustomerTimeLine) {
		this.getCustomerTimeLine = getCustomerTimeLine;
	}

	public GetClientTimeLine getGetClientTimeLine() {
		return getClientTimeLine;
	}

	public void setGetClientTimeLine(GetClientTimeLine getClientTimeLine) {
		this.getClientTimeLine = getClientTimeLine;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public void setCustomerList(CustomerList_V2 customerList) {
		this.customerList = customerList;
	}

	public CustomerList_V2 getCustomerList() {
		return customerList;
	}

	public CrmCategory getCrmCategory() {
		return crmCategory;
	}

	public void setCrmCategory(CrmCategory crmCategory) {
		this.crmCategory = crmCategory;
	}

	public Customer_V2 getCustomer() {
		return customer;
	}

	public void setCustomer(Customer_V2 customer) {
		this.customer = customer;
	}

	public MessageListRes getMessageLists() {
		return messageLists;
	}

	public void setMessageLists(MessageListRes messageLists) {
		this.messageLists = messageLists;
	}

	public CustomerInfo getTemp() {
		return temp;
	}

	public void setTemp(CustomerInfo temp) {
		this.temp = temp;
	}

	@Override
	public void clear() {
	}

	@Override
	public void releaseResource() {
	}

	/**
	 * 添加异动备注
	 * 
	 * @param context
	 * @param info
	 * @param reason
	 */
	

	// CompanyWorkLineActivity.java
	public void getClientTimeLine(CustomerInfo curCustomer) {
		getClientTimeLine = new GetClientTimeLine(curCustomer);
		getClientTimeLine.execute();
	}

	// CustomersActivity.java
	public void getCustomersCmdExecuted(String s1) {
		new GetCustomersCmd(s1).execute();
	}

	public void getCustomersCmdExecuted(String s1, String s2, int i) {
		new GetCustomersCmd(s1, s2, i).execute();
	}

	// CustomerTimeLineActivity.java
	public void getCustomerTimeLineExecuted(CustomerInfo curCustomer) {
		getCustomerTimeLine = new GetCustomerTimeLine(curCustomer);
		getCustomerTimeLine.execute();
	}

	// EditCustomerActivity.java
	public String getAppCategory_INDUSTRY() {
		return GetAppCategory.TYPE_INDUSTRY;
	}

	public String getAppCategory_APP() {
		return GetAppCategory.TYPE_APP;
	}

	// //////////////////////////////////////////////////////////////////
	// ConsultHistoryActiviyt.java
	public void getChatHistory(String conversationId, int i) {
		new GetChatHistory(conversationId, i).execute();
	}

	// CustomerDetailActivity.java
	public void handleGetCustomerById(Context ctx, String chatCustomerId) {
		new GetCustomerById(ctx, chatCustomerId).execute();
	}

	// CustomerDistributeActiivty.java
	public void assignCustomer(CustomerInfo customerInfo, String reason,
			String userId) {
		new AssignCustomer(customerInfo, reason, userId).execute();
	}



	// CustomerActivity.java
	public void getCustomersCmd(String string, final int currentSize) {
		new GetCustomersCmd(string, currentSize).execute();
	}

	// CustomerTimelineActivity.java
	public void getCustomerTimeLine(CustomerInfo curCustomer) {
		new GetCustomerTimeLine(curCustomer).execute();
	}

	// EditCustomerActivity.java
	public void getAppCategory(String s, int i) {
		new GetAppCategory(s, i).execute();
	}

	public void updateCustomer(CustomerInfo customerInfo) {
		new UpdateCustomer(customerInfo).execute();
	}

	// ///////////////////////////////////////////以下为原CrmDataManager.java//////////////////////////////////////
	public static String LIST_REFRESH = "LIST_REFRESH";
	/** 只是做暂时缓存使用，比如Intent之间的跳转 */
	private List<CustomerInfo> list = new ArrayList<CustomerInfo>();
	private List<CustomerInfo> matchsList = new ArrayList<CustomerInfo>();

	public List<CustomerInfo> getList() {
		return list;
	}

	public List<CustomerInfo> getMatchCustomerList() {
		return matchsList;
	}

	public CustomerInfo getCustomerById(String id) {
		for (CustomerInfo info : list) {
			if (info.getCustomer().getId().equals(id))
				return info;
		}
		return null;
	}

	public void removeCustomerById(String id) {
		for (int i = 0; i < list.size(); i++) {
			CustomerInfo info = list.get(0);
			if (info.getCustomer().getId().equals(id)) {
				list.remove(info);
				return;
			}
		}
	}

	public void addCustomer(Customer_V2 value) {
		CustomerInfo info = new CustomerInfo();
		info.setCustomer(value);
		list.add(info);
		noticeListener(LIST_REFRESH);
	}

	public void addMatchCustomer(Customer_V2 value) {
		CustomerInfo info = new CustomerInfo();
		info.setCustomer(value);
		matchsList.add(info);
	}

	public void addMatchCustomerList(List<Customer_V2> value) {
		for (Customer_V2 cus : value) {
			CustomerInfo info = new CustomerInfo();
			info.setCustomer(cus);
			matchsList.add(info);
		}
	}

	public void addCustomerList(List<Customer_V2> value) {
		for (Customer_V2 cus : value) {
			CustomerInfo info = new CustomerInfo();
			info.setCustomer(cus);
			list.add(info);
		}
		noticeListener(LIST_REFRESH);
	}

	public void deleteCustomer(CustomerInfo info) {
		list.remove(info);
		noticeListener(LIST_REFRESH);
	}

	public void clearList() {
		list.clear();
		noticeListener(LIST_REFRESH);
	}

	public void clearMatchList() {
		matchsList.clear();
	}
	// /////////////////////////////////////////////////////////////////////////////////////////////////////
}
